package com.learn.springcloud.hystrixexplainindetailconsumer.command.naming.service;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import org.springframework.web.client.RestTemplate;

/**
 * @Time : 2018/8/3 17:38
 * @Version : 1.0
 * @Author : hongyan.li@hand-china.com
 * Copyright @ 2000 Shanghai Hand Co. Ltd.
 * All right reserved.
 */
public class CommandNamingService1 extends HystrixCommand<String> {

    private RestTemplate restTemplate;
    private String name;

    protected CommandNamingService1(RestTemplate restTemplate, String name) {
        super(Setter
             .withGroupKey(
                HystrixCommandGroupKey.Factory.asKey("Command Naming Group")
            )
            .andCommandKey(
                HystrixCommandKey.Factory.asKey("Command Naming 1")
            )
        );
        this.restTemplate = restTemplate;
        this.name = name;
    }

    @Override
    protected String run() throws Exception {
        System.out.println("UserServiceException Running : " + name);
        String url = "http://hystrix-explain-in-detail-provider/request/command/say/hello?name={1}";
        return restTemplate.getForObject(url, String.class, name);
    }

    @Override
    protected String getFallback(){
        return "error, " + this.name;
    }

}
